/**
 * Created by xuxujunjunforever on 2015/12/21.
 */
'use strict'

let models = require('../../../modules/models');
let config = require('../../../config');
let privilegeActions = require('../../../modules/actions/privilege');
let Fibers = require('fibers');

let PrivilegeDao = models.PrivilegeDao;

class Response {
    json(obj) {
        this.result = obj;
    }
}
let session = {};

function actionCreatePrivilegeNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            privilegeActions.actionCreatePrivilege(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionCreatePrivilegeSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "privilege": {
                    name: "test"
                }
            },
            "session": session
        };
        let res = new Response();

        privilegeActions.actionCreatePrivilege(req, res);
        test.ok(res.result.successful,'create failed')

        let privilege = PrivilegeDao.findOne({
            id:res.result.data.privilege.id
        });
        let issame = false;
        if (privilege.name == req.body.privilege.name) {
            issame = true;
        }

        test.ok(issame,'they should be equal');

        test.done();
    }).run();
}

function actionRefreshPrivilegeNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            privilegeActions.actionRefreshPrivilege(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRefreshPrivilegeNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "privilege":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            privilegeActions.actionRefreshPrivilege(req,res);
        },config.errors.classes.PrivilegeCanNotFound);

        test.done();
    }).run();
}

function actionRefreshPrivilegeSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "privilege": {
                    "id":"5677704c879382401e611e91"
                }
            },
            "session": session
        };
        let res = new Response();

        privilegeActions.actionRefreshPrivilege(req, res);
        test.ok(res.result.successful,'refresh failed')

        let privilege = PrivilegeDao.findOne({
            id:res.result.data.privilege.id
        });

        test.deepEqual(privilege.toObject({recursive:true}),res.result.data.privilege,'they should be equal');

        test.done();
    }).run();
}

function actionRemovePrivilegeNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            privilegeActions.actionRemovePrivilege(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRemovePrivilegeNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "privilege":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            privilegeActions.actionRemovePrivilege(req,res);
        },config.errors.classes.PrivilegeCanNotFound);

        test.done();
    }).run();
}

function actionRemovePrivilegeSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "privilege": {
                    "id":"56777d25a59900344a4665b9",
                }
            },
            "session": session
        };
        let res = new Response();

        privilegeActions.actionRemovePrivilege(req, res);
        test.ok(res.result.successful,'remove failed')

        let privilege = PrivilegeDao.findOne({
            id:req.body.privilege.id
        });

        test.deepEqual(privilege,null,'privilege should not be found');

        test.done();
    }).run();
}


module.exports = {
    setUp: function (callback){
        session = {
            "login":{
                "user": {
                    "id":"5667cbd68e731ba02164525d",
                    "name": "test",
                    "password": "e10adc3949ba59abbe56e057f20f883e"
                }
            }
        };
        callback();
    },
    actionCreatePrivilegeNotLogin,
    actionCreatePrivilegeSuccessful,
    actionRefreshPrivilegeNotLogin,
    actionRefreshPrivilegeNotFound,
    actionRefreshPrivilegeSuccessful,
    //actionRemovePrivilegeNotLogin,
    //actionRemovePrivilegeNotFound,
    //actionRemovePrivilegeSuccessful
};
